﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.EntityModel;
using iPick.Business;
using System.Web.Helpers;
using iPick.Models;
using System.Web.Security;
using iPick.Business.Common;
namespace iPick.Controllers
{
    public class HomeController : Controller
    {
        protected iPick.Models.Application ApplicationModel;

        public ActionResult Start()
        {
            return View();
        }

        [Header("Random Movies")]
        public ActionResult Index(string dkey)
        {
            iPick.Models.Application _Application = new Models.Application();
            iPick.Business.ApplicationModelHelper.SetSession();
            ApplicationModel = (iPick.Models.Application)(Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel]);
         //   ApplicationModel = iPick.Business.ApplicationModelHelper.getModelPopulated(ApplicationModel);
            Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] = ApplicationModel;
            return View(ApplicationModel);
        }




        [Header("Movie Details")]
        public ActionResult IndexDetails(int? Id)
        {
            if (Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] == null)
                iPick.Business.ApplicationModelHelper.SetSession();

            ApplicationModel = (iPick.Models.Application)(Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel]);
            ApplicationModel.Catalog = ApplicationModel.lCatalog.Find(s => s.CatalogId == Id);
            //Scenario where session is expired and we dont have the movie in the catalog
            if (ApplicationModel.Catalog == null)
            {
                dbSQLIPickEntities _db = new iPick.dbSQLIPickEntities();
                ApplicationModel.Catalog = _db.Catalogs.FirstOrDefault(p => p.CatalogId == Id);
            }
            return View(ApplicationModel);
        }



        [Header("Filter Options")]
        public ActionResult Filter()
        {

            if (Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] == null)
                iPick.Business.ApplicationModelHelper.SetSession();

            ApplicationModel = (iPick.Models.Application)(Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel]);
            ApplicationModel.FilterOptions = new List<FilterOptionsModel>();
            // Creaate Filter ChkList Options
            

            var filterOptions = new FilterOptionsModel
            {
                Id = "1",
                isSelected = false,
                option = "G"
            };
            ApplicationModel.FilterOptions.Add(filterOptions);
            filterOptions = new FilterOptionsModel
            {
                Id = "2",
                isSelected = false,
                option = "PG"
            };
            ApplicationModel.FilterOptions.Add(filterOptions);
            filterOptions = new FilterOptionsModel
            {
                Id = "3",
                isSelected = false,
                option = "PG-13"
            };
            ApplicationModel.FilterOptions.Add(filterOptions);
            //ApplicationModel.Filter.filterOption = new UserRatingModel();
            //ApplicationModel.Filter.filterOption=filterOption;
            
            return View(ApplicationModel);
           
          
        
        }

        [HttpPost]
        public ActionResult Filter(iPick.Models.Application ApplicationFomObject )
        {
            if (Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] == null)
                iPick.Business.ApplicationModelHelper.SetSession();

            ApplicationModel = (iPick.Models.Application)(Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel]);
            ApplicationModel.FilterOptions = ApplicationFomObject.FilterOptions;
            return View("Index",ApplicationModel);
        }


        [Header("About")]
        public ActionResult About()
        {
            ViewBag.Message = "Your quintessential app description page.";

            return View();
        }

        public ActionResult ThankYou()
        {
            ViewBag.Message = "Thank You";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Contact Page";

            return View();
        }

        public ActionResult ContactSent()
        {

            return View();

        }

        [HttpPost]
        public ActionResult Contact(FormCollection myForm)
        {
            if (ModelState.IsValid)
            {
                try
                {

                    WebMail.SmtpServer = "mail.ipickmovies.com";
                    WebMail.SmtpPort = 25;

                    WebMail.Password = "sletuspickit1!";
                    WebMail.UserName = "support@ipickmovies.com";
                    WebMail.SmtpUseDefaultCredentials = true;

                    WebMail.Send(

                              myForm["Email"],

                            myForm["Subject"],

                            myForm["Body"],

                            "support@ipickmovies.com", "support@ipickmovies.com"

                        );



                    return RedirectToAction("ThankYou");

                }

                catch (Exception ex)
                {

                    ViewData.ModelState.AddModelError("_FORM", ex.ToString());
                    return View();
                }

            }
            else
                return View();

        }


        #region Events

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            ApplicationModel = (iPick.Models.Application)(Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel]);
            ApplicationModel = iPick.Business.ApplicationModelHelper.getModelPopulated(ApplicationModel);
            Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] = ApplicationModel;

            return View(ApplicationModel);
        }


       

        //Added by M.A. Nov.1.2012
        public ActionResult RemoveFavorites(long customerFavoriteID)
        {
            //RefreshCurrentApplicationModel();
            iPick.Controllers.DataServiceController dataServices = new DataServiceController();

            //if (ApplicationModel.Customer == null)
            //    ApplicationModel.Customer = dataServices.getCustomerbyEmailAddress(User.Identity.Name);

            dataServices.CustomerFavorites_Remove(customerFavoriteID);

            return View();
        }

        private void RefreshCurrentApplicationModel()
        {
            if (Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel] == null)
                iPick.Business.ApplicationModelHelper.SetSession();

            ApplicationModel = (iPick.Models.Application)(Session[iPick.Business.ApplicationModelHelper.Keys.ApplicationModel]);

        }

        #endregion
    }
}
